//
//  ServiceRootViewController.swift
//  KaMengApplication
//
//  Created by DUONIU_MAC on 2018/10/31.
//  Copyright © 2018年 DUONIU_MAC. All rights reserved.
//

import UIKit

class ServiceRootViewController: KMViewController {
    
    //综合资讯
    fileprivate var headLineView = ServiceHeadlineView()
    
    fileprivate var serviceVC : ServiceRootNewsViewController!

    override func viewDidLoad() {
        super.viewDidLoad()

        addSubView()
        NotificationCenter.default.addObserver(self, selector: #selector(showHeadline), name: .servicePush, object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(amountRaisedPush(sender:)), name: .amountRaisedPush, object: nil)
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        
    }
    
    deinit {
        
        NotificationCenter.default.removeObserver(self, name: .servicePush, object: nil)
        NotificationCenter.default.removeObserver(self, name: .amountRaisedPush, object: nil)
    }
    
    //MARK: 提额
    @objc fileprivate func amountRaisedPush(sender:Notification) {
        
        let index = Int((sender.userInfo!["index"] as? String)!)
        self.moveToViewController(index: index!)
    }
    
    //MARK: 显示综合模块
    @objc fileprivate func showHeadline() {
        
        headLineView.isHidden = false
        self.view.bringSubview(toFront: headLineView)
    }
    
    //MARK: 添加视图
    fileprivate func addSubView() {
        
        headLineView.headView.moreButton.addTarget(self, action: #selector(moreLabel), for: .touchUpInside)
        headLineView.urlString = url_serviceNews
        headLineView.delegate = self
        headLineView.titleArray = [pinCard,late,bill,activated,annualFee,blacklist,handlingFee,integral,changeCard,preferential,amountRaise,hotel,tourism,consumption,popularBank,seaAmoy,safety,overdraft,cost,application,stages,repayment,cashWithdrawal,loss,creditCardLimit,rersonalCredit,integral,shopping]
        
        self.view.addSubview(headLineView)

        
        headLineView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview().inset(UIEdgeInsets.zero)
        }
        
        
    }
    
    //MARK: 更多标签
    @objc fileprivate func moreLabel() {
        
        let dataArray = [pinCard,late,bill,activated,annualFee,blacklist,handlingFee,integral,changeCard,preferential,amountRaise,hotel,tourism,consumption,popularBank,seaAmoy,safety,overdraft,cost,application,stages,repayment,cashWithdrawal,loss,creditCardLimit,rersonalCredit,integral,shopping]
        
        let moreLabelVC = MoreLabelViewController.init(dataArray: dataArray)
        moreLabelVC.labelBlock = {(index) in
            self.moveToViewController(index: index.row)
        }
        self.present(moreLabelVC, animated: false, completion: nil)
    }
    
    //AMRK: 创建新闻详情页
    public func moveToViewController(index:Int) {
        
        //移除综合新闻
        headLineView.isHidden = true
        
        if serviceVC == nil {
            serviceVC = ServiceRootNewsViewController()
            self.addChildViewController(serviceVC)
            self.didMove(toParentViewController: self)
            
            self.view.addSubview(serviceVC.view)
            
            serviceVC.view.snp.makeConstraints { (make) in
                make.edges.equalToSuperview().inset(UIEdgeInsets.zero)
            }
        }
        
        serviceVC.moveToViewController(at: index)
        
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()

        

    }
    

    

}

extension ServiceRootViewController : ServiceHeadlineViewDelegate {
    
    
    //MARK: 选择标签
    func selectItem(index: Int) {
        
        self.moveToViewController(index: index)
    }
    
    //MARK: 选择新闻
    func selectNews(model: ServiceNewsModel) {
        
        let webVC = WebViewController.init(urlString: kmHttps + model.href!, title: service_title + synthesis, shareImage: nil, shareText: model.text, shareTitle: model.title)
        webVC.hidesBottomBarWhenPushed = true
        webVC.removeElements = ["mblock mblocks","crumbs mblock","mfooter mblock footer1"]
        webVC.removeIds = ["xiong_div"]
        self.navigationController?.pushViewController(webVC, animated: true)
    }
}
